package com.yunfei.api.message;
import com.yunfei.api.message.contants.AppMessageInformationCenterContants;
import com.yunfei.api.message.param.information.findAllList.MessageInformationFindAllDataListParam;
import com.yunfei.api.message.param.information.update.MessageInformationUpdateParam;
import com.yunfei.api.message.responseBody.templage.initPageData.MessageTemplateInitPageData;
import com.yunfei.bo.messageSendDtl.MessageSendDtlSimpleBo;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.bo.tDictionary.TdictionarySimpleBo;
import com.yunfei.enums.ResultStatus;
import com.yunfei.fegin.RedisFeignService;
import com.yunfei.mapper.aiiidb.MessageSendDtlMapper;
import com.yunfei.mapper.aiiidb.MsgRuleConfigMapper;
import com.yunfei.mapper.aiiidb.TPersonalUserMapper;
import com.yunfei.mapper.aiiidb.TdictionaryMapper;
import com.yunfei.ribbon.RedisServiceRibbon;
import com.yunfei.service.RedisService;
import com.yunfei.utils.page.PageCommon;
import com.yunfei.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 消息信息管理
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@Api(value = AppMessageInformationCenterContants.MESSAGE_INFORMATION_CENTER_API_NAMESPACE,tags = AppMessageInformationCenterContants.MESSAGE_INFORMATION_CENTER_API_NAMESPACE )
@RestController
@RequestMapping("/api/message")
public class AppMessageInformationCenterApiController {
    @Autowired
    private TPersonalUserMapper tPersonalUserMapper;
    @Autowired
    private RedisFeignService redisFeignService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisServiceRibbon redisServiceRibbon;
    @Autowired
    private MsgRuleConfigMapper msgRuleConfigMapper;
    @Autowired
    private MessageSendDtlMapper messageSendDtlMapper;
    @Autowired
    private TdictionaryMapper tdictionaryMapper;
    @Autowired
    private Environment env;
    /**
    * @Description: 页面初始化
    * @Author: zhengyunfei
    * @Date: 2019/2/25
    * @time:11:38
    */
    @GetMapping(value = "/information/initPageData",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = AppMessageInformationCenterContants.INIT_PATE_DATA,notes = AppMessageInformationCenterContants.AUTHOR_NOTE,position = 1,response = MessageTemplateInitPageData.class)
    public Object initPage(){
        ResultModel resultModel=new ResultModel();
        try{
            Map<String,Object> resultMap=new HashMap<>();
            //发送类型
            List<TdictionarySimpleBo> sendType=tdictionaryMapper.getSendType();
            //发送状态
            List<TdictionarySimpleBo> sendStatus=tdictionaryMapper.getSendStatus();

            resultMap.put("sendType",sendType);
            redisService.set("sendType",sendType);
            resultMap.put("sendStatus",sendStatus);
            redisService.set("sendStatus",sendStatus);
            resultModel.setData(resultMap);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }
        return resultModel;
    }
    /**
    * @Description: 查询列表
    * @Author: zhengyunfei
    * @Date: 2019/2/25
    * @time:11:38
    */
    @PostMapping(value = "/information/initPageData",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = AppMessageInformationCenterContants.FIND_ALL_LIST_DATA,notes = AppMessageInformationCenterContants.AUTHOR_NOTE,position = 1,response = MessageTemplateInitPageData.class)
    public Object findAllList(MessageInformationFindAllDataListParam param){
        ResultModel resultModel=new ResultModel();
        try{
            Map<String,Object> resultMap=new HashMap<>();
            String msgClass=param.getMsgClass();
            String sendType=param.getSendType();
            String sendState=param.getSendState();
            String pageNow=param.getPageNow();
            int page=1;
            if(!StringUtil.isNullOrEmpty(pageNow)){
                page=Integer.parseInt(pageNow);
            }
            int pageSize= PageCommon.PAGE_SIZE;
            Map<String,Object> queryMap=new HashMap<>();
            queryMap.put("msgClass",msgClass);
            queryMap.put("sendType",sendType);
            queryMap.put("sendState",sendState);
            queryMap.put("msgClass",msgClass);
            queryMap.put("curNo",(page-1)*pageSize);
            queryMap.put("curSize",page*pageSize);
            List<MessageSendDtlSimpleBo> list=messageSendDtlMapper.findAllListPage(queryMap);
            resultMap.put("result",list);
            resultModel.setData(resultMap);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }
        return resultModel;
    }

    /**
     * @Description: 更新
     * @Author: zhengyunfei
     * @Date: 2019/2/25
     * @time:11:38
     */
    @PostMapping(value = "/information/update",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = AppMessageInformationCenterContants.UPDATE,notes = AppMessageInformationCenterContants.AUTHOR_NOTE,position = 1,response = MessageTemplateInitPageData.class)
    public Object update(MessageInformationUpdateParam param){
        ResultModel resultModel=new ResultModel();
        try{
            Map<String,Object> resultMap=new HashMap<>();
            Map<String,Object> updateParam=new HashMap<>();
            String msgId=param.getMsgId();
            String msgContext=param.getMsgContext();
            updateParam.put("msgId",msgId);
            updateParam.put("msgContext",msgContext);
            messageSendDtlMapper.update(updateParam);
            resultMap.put("result","更新成功");
            resultModel.setData(resultMap);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }
        return resultModel;
    }
    /**
     * @Description: 手动发送消息
     * @Author: zhengyunfei
     * @Date: 2019/2/25
     * @time:11:38
     */
    @PostMapping(value = "/information/handToSendMessage/initPage",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = AppMessageInformationCenterContants.HAND_TO_MESSAGE_INIT_PAGE,notes = AppMessageInformationCenterContants.AUTHOR_NOTE,position = 1,response = MessageTemplateInitPageData.class)
    public Object handToSendMessage(MessageInformationUpdateParam param){
        ResultModel resultModel=new ResultModel();
        try{
            Map<String,Object> resultMap=new HashMap<>();
            //消息分类
            List<TdictionarySimpleBo> sendType=tdictionaryMapper.getMessageClass();

            resultModel.setData(resultMap);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }
        return resultModel;
    }
}
